package com.example.permissionserviceconsumer.controller;

import com.myprojects.common.pojo.UserWithRole;
import com.example.permissionserviceconsumer.service.FeignUserWithRoleService;
import com.myprojects.common.dto.Msg;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/user-curd")
public class UserWithRoleController {

    @Autowired
    FeignUserWithRoleService feignUserWithRoleService;

    /**
     * 该方法用于获取全部的角色
     */
    @RequestMapping(value = "/roles", method = RequestMethod.GET)
    @ResponseBody
    public Msg getAllRoles() {
        return feignUserWithRoleService.getAllRoles();
    }

    /**
     * 该方法用于分页查询所有的用户信息(非员工)
     */
    @RequestMapping(value = "/users", method = RequestMethod.GET)
    @ResponseBody
    public Msg selectUsersByPageNum(
            @RequestParam(value = "pn", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "size", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "search", defaultValue = "") String searchStr
    ) {//普通
        return feignUserWithRoleService.selectUsersByPageNum(pageNum,pageSize,searchStr);
    }

    /**
     * 该方法用于检查保存用户的信息
     */
    @RequestMapping(value = "/check-user", method = RequestMethod.POST)
    @ResponseBody
    public Msg checkNewUser(@RequestBody UserWithRole user) {
        return feignUserWithRoleService.checkNewUser(user);
    }

    /**
     * 该方法用于检查更新用户的信息
     */
    @RequestMapping(value = "/check-user", method = RequestMethod.PUT)
    @ResponseBody
    public Msg checkUpdateUser(@RequestBody UserWithRole user) {
        return feignUserWithRoleService.checkUpdateUser(user);
    }

    /**
     * 该方法用于保存用户
     */
    @RequestMapping(value = "/users", method = RequestMethod.POST)
    @ResponseBody
    public Msg saveUser(@RequestBody UserWithRole user) {
        return feignUserWithRoleService.saveUser(user);
    }

    /**
     * 该方法用于更新用户(该方法会更新用户所拥有的角色)
     */
    @RequestMapping(value = "/users", method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateUser(@RequestBody UserWithRole user) {
        return feignUserWithRoleService.updateUser(user);
    }

//    /**
//     * 该方法用于更新用户(该方法不会更新用户所拥有的角色)
//     */
//    @RequestMapping(value = "/users_without_role", method = RequestMethod.PUT)
//    @ResponseBody
//    public Msg updateUserWithoutRole(@RequestBody UserWithRole user) {
//        return feignUserWithRoleService.updateUserWithoutRole(user);
//    }
//
//    /**
//     * 该方法用于检查更新用户的信息
//     */
//    @RequestMapping(value = "/users_without_role", method = RequestMethod.POST)
//    @ResponseBody
//    public Msg checkUserWithoutRole(@RequestBody UserWithRole user) {
//        return feignUserWithRoleService.checkUserWithoutRole(user);
//    }

    /**
     * 该方法用于一次删除多个用户
     */
    @RequestMapping(value = "/users", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteLotsUser(@RequestParam List<Integer> idList) {
        return feignUserWithRoleService.deleteLotsUser(idList);
    }


}
